<template>
  <!-- 解决商品价格及名称闪烁问题，判断是否有goods_name属性，有渲染页面，没有隐藏 -->
  <view v-if="goods_info.goods_name" class="goods-box">
   <swiper :indicator-dots="true" :autoplay="true" :interval="1500" :duration="1000" circular="true">
     <swiper-item v-for="(item , i) in goods_info.pics" :key="i">
       <image :src="item.pics_big" @click="previewImg(i)"></image>
     </swiper-item>
   </swiper>
   <!-- 商品信息模块 -->
   <view class="goos_detail_info">
     <!-- 商品价格 -->
     <view class="price">
       ￥{{goods_info.goods_price}}
     </view>
     <!-- 商品信息 -->
     <view class="goods-info-body">
      <view class="goods_name">
         {{goods_info.goods_name}}
      </view>
       <view class="favia">
         <uni-icons type="star" size="16"></uni-icons>
         <text>收藏</text>
       </view>
     </view >
     <!-- 运费 -->
     <view class="yunf">
        快递：免运费
     </view>
   </view>
   <rich-text :nodes="goods_info.goods_introduce"></rich-text>
   <!-- 商品导航 -->
   <view class="goods-nav">
     <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
   </view>
   
  </view>
</template>

<script>
  // 从 vuex 中按需导出 mapState 辅助方法
  import { mapState ,mapMutations ,mapGetters} from 'vuex'
  export default {
    computed:{
      // ...mapState('模块的名称', ['要映射的数据名称1', '要映射的数据名称2'])
         ...mapState('m_cart', ['cart']),
          ...mapGetters('m_cart', ['total']),
    },
    watch:{
      // total (newVal){
        //函数形式侦听器页面首次加载不会调用，采用对象形式
        total:{
          handler(newVal){
            const findResult = this.options.find((x) => x.text === '购物车')
                 if (findResult) {
                   // 3. 动态为购物车按钮的 info 属性赋值
                   findResult.info = newVal
                 }
          },
          immediate:true
      }
    },
    data() {
      return {
      goods_info:{},
         options: [{
      			icon: 'shop',
      			text: '店铺',
      			infoBackgroundColor:'#007aff',
      			infoColor:"red"
      		}, {
      			icon: 'cart',
      			text: '购物车',
      			info: 2
      		}],
      	    buttonGroup: [{
      	      text: '加入购物车',
      	      backgroundColor: '#ff0000',
      	      color: '#fff'
      	    },
      	    {
      	      text: '立即购买',
      	      backgroundColor: '#ffa200',
      	      color: '#fff'
      	    }
      	    ]
      };
    },
    onLoad(options) {
      // console.log(options.goods_id);
      this.getGoodsDetail(options.goods_id)
      // this.getinfo()
    },
    methods: {
      onClick(e){
        // console.log(e.content.text);
        if(e.content.text == "购物车"){
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      async getGoodsDetail(goods_id) {
        const {data: res} = await uni.$http.get('/goods/detail?goods_id=' + goods_id)
        console.log(res);
        if(res.meta.status !==200) {
          return uni.showMsg()
        }
        //解决图片底部空隙问题，给图片添加display:block;属性
       res.message.goods_introduce= res.message.goods_introduce.replace(/<img /g , '<img style = "display : block;"').replace(/webp/g , 'jpg')
        this.goods_info = res.message
      },
      previewImg(i){
        uni.previewImage({//uni.previewImage方法开启图片预览
          current:i ,//指定预览图片默认为当前索引
          urls:this.goods_info.pics.map(x => x.pics_big) //所有图片的url地址
        })
      },
       ...mapMutations('m_cart',['addGoods']),
      buttonClick(e){
        // console.log(e);
        if(e.content.text==='加入购物车'){
         const goodDetail ={
           goods_id:this.goods_info.goods_id,
           goods_name: this.goods_info.goods_name,
           goods_price: this.goods_info.goods_price,
           goods_count: 1,
           goods_small_logo:this.goods_info.goods_small_logo,
           goods_state:true
         }
         this.addGoods(goodDetail)
        }
      },
      //购物车总数量
      // getinfo(){
      //   const c=0
      //  this.options[1].info= this.cart.forEach(item => c += item.goods_count)
      // }
    }
  }
</script>

<style lang="scss">
swiper {
  height: 750rpx;
  image {
    width: 100%;
    height: 100%;
  }
}
.goos_detail_info {
  .price{
    font-size: 18px;
    padding: 10px 0;
    color: #C00000;
  }
  .goods-info-body {
    display: flex;
    justify-content: space-between;
    .goods_name{
      font-size: 13px;
      margin-right: 10px;
    }
    .favia {
      width: 120px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      font-size: 12px;
      align-items: center;
      color: gray;
      border-left: 1px solid #efefef;
    }
  }
  .yunf {
    color: gray;
    margin: 10px 0;
    font-size: 12px;
  }
}
.goods-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
.goods-box {
  padding-bottom: 50px;
}
</style>
